package com.xz.server.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xz.comment.utils.JsonResult;
import com.xz.db.core.PagePlus;
import com.xz.db.core.PageRequest;
import com.xz.db.entity.XzBook;
import com.xz.server.dto.BookReq;
import com.xz.server.dto.BookUpdateReq;
import com.xz.server.service.XzBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.List;

/**
 * BookController
 *
 * @author 28310
 * @since 2023-11-01 16:28
 */

@RestController
@Api(tags = "记事本接口")
@RequestMapping("/api/book")
@Slf4j
public class BookController {


    @Resource
    private XzBookService xzBookService;

    @PostMapping("/list")
    @ApiOperation("1.记事本列表")
    public JsonResult<IPage<XzBook>> pageList(@RequestBody PageRequest request){
        return JsonResult.success(xzBookService.pageVo(new PagePlus<>(request),null,XzBook.class));
    }

    @PostMapping("/add")
    @ApiOperation("2.记事本新增")
    public JsonResult<Void> add(@Validated @RequestBody BookReq bookReq){
        XzBook bean = BeanUtil.toBean(bookReq, XzBook.class);
        bean.setTime(LocalDateTime.now());
        boolean save = xzBookService.save(bean);
        return save?JsonResult.success():JsonResult.error();
    }


    @PostMapping("/update")
    @ApiOperation("3.记事本修改")
    public JsonResult<Void> update(@Validated @RequestBody BookUpdateReq bookReq){
        boolean save = xzBookService.updateById(BeanUtil.toBean(bookReq, XzBook.class));
        return save?JsonResult.success():JsonResult.error();
    }


    @PostMapping("/del/{id}")
    @ApiOperation("3.记事本删除")
    public JsonResult<Void> delete(@PathVariable(value = "id") @NotNull Long id){
        boolean b = xzBookService.removeById(id);
        return b?JsonResult.success():JsonResult.error();
    }

    @PostMapping("/delList")
    @ApiOperation("4.记事本批量删除")
    public JsonResult<Void> deleteList(@RequestBody @NotEmpty List<Long> ids){
        boolean b = xzBookService.removeBatchByIds(ids);
        return b?JsonResult.success():JsonResult.error();
    }



}
